const router = require('koa-router')()
let sqlQuery = require('../db');
router.prefix('/users')

router.get('/',async function (ctx, next) {
  ctx.body = 'this is a users response!'
})

router.get('/bar', function (ctx, next) {
  ctx.body = 'this is a users/bar response'
})

router.post('/addUser', async (ctx, next) => {
    try {
        let { code,nickName,avatarUrl } = ctx.request.body;
        let sql = 'insert into users(code,nickName,avatarUrl) values(?,?,?)';
        let result = await sqlQuery(sql, [code, nickName,avatarUrl]);
        ctx.body = {
            state: 200,
            message:result
        }
    } catch (error) {
        next(error);
    }
})
// 我的获取用户信息
router.get('/getUser', async (ctx, next) => {
    try {
        let { code } = ctx.query;
        let result = await sqlQuery('SELECT * FROM users WHERE code=? AND state=1', [code]);
        await Promise.all(result.map(async item => {
            // 用户获赞粉丝情况
            let praiseList = await sqlQuery(`SELECT praise.other_user_id FROM happy_video JOIN praise ON happy_video.id=praise.video_id AND happy_video.state=1 AND praise.user_id=?`, [item.id]);
            let praiseNum = await sqlQuery(`SELECT COUNT(*) count FROM happy_video JOIN praise ON happy_video.id=praise.video_id AND happy_video.state=1 AND praise.user_id=?`, [item.id]);
            let otherUserList = await sqlQuery(`SELECT other_user_id FROM follow WHERE state=1 AND user_id=?`, [item.id]);
            let otherUserNum = await sqlQuery(`SELECT COUNT(*) count FROM follow WHERE state=1 AND user_id=?`, [item.id]);
            let fensList = await sqlQuery(`SELECT user_id FROM follow WHERE other_user_id=? AND state=1`, [item.id]);
            let fensNum = await sqlQuery(`SELECT COUNT(*) count FROM follow WHERE other_user_id=? AND state=1`, [item.id]);
            item.info = {
                praise:{
                    praiseList,
                    count:praiseNum[0].count
                },
                otherUser: {
                    otherUserList,
                    count:otherUserNum[0].count
                },
                fens: {
                    fensList,
                    count:fensNum[0].count
                }
            }
            let videoList = await sqlQuery(`SELECT id,title,url,navigator_url,class_id FROM happy_video WHERE state=1 AND user_id=?`, [item.id]);
            let myVideosNum = await sqlQuery(`SELECT COUNT(*) count FROM happy_video WHERE state=1 AND user_id=?`, [item.id]);
            let privateList = await sqlQuery(`SELECT id,title,url,navigator_url,class_id FROM happy_video WHERE state=0 AND user_id=?`, [item.id]);
            let privateNum = await sqlQuery(`SELECT COUNT(*) count FROM happy_video WHERE state=0 AND user_id=?`, [item.id]);
            let favoriteList = await sqlQuery(`SELECT happy_video.* FROM happy_video JOIN praise ON happy_video.id=praise.video_id AND happy_video.state=1 AND praise.other_user_id=?`, [item.id]);
            let favoriteNum = await sqlQuery(`SELECT COUNT(*) count FROM happy_video JOIN praise ON happy_video.id=praise.video_id AND happy_video.state=1 AND praise.other_user_id=?`, [item.id]);
            let collectList = await sqlQuery(`SELECT happy_video.* FROM happy_video JOIN collect ON happy_video.id=collect.video_id AND happy_video.state=1 AND collect.other_user_id=?`, [item.id]);
            let collectNum = await sqlQuery(`SELECT COUNT(*) count FROM happy_video JOIN collect ON happy_video.id=collect.video_id AND happy_video.state=1 AND collect.other_user_id=?`, [item.id]);
            item.footInfo = {
                videos:{
                    videoList,
                    count:myVideosNum[0].count
                },
                private:{
                    privateList,
                    count:privateNum[0].count
                },
                favorite: {
                    favoriteList,
                    count:favoriteNum[0].count
                },
                collect: {
                    collectList,
                    count:collectNum[0].count
                }
            }
        }))
        ctx.body = {
            state: 200,
            message:result
        }
    } catch (error) {
        next(error)
    }
})
// 获取好友
// async function getOtherUsers(value,result) {
//     await Promise.all(value.otherUser.otherUserList.map(async item => {
//         let sql = `SELECT id,avatarUrl,nickName,(SELECT COUNT(*) count FROM messages WHERE userid=? AND state=0) comment FROM users WHERE state=1 AND id=?`;
//         let user = await sqlQuery(sql, [item.other_user_id,item.other_user_id]);
//         result.otherUser.push(user[0]);
//     }))
// }
// // 获取粉丝
// async function getFens(value,result) {
//     await Promise.all(value.fens.fensList.map(async item => {
//         let sql = `SELECT id,avatarUrl,nickName,(SELECT COUNT(*) count FROM messages WHERE userid=? AND state=0) comment FROM users WHERE state=1 AND id=?`;
//         let user = await sqlQuery(sql, [item.user_id,item.user_id]);
//         result.fens.push(user[0]);
//     }))
// }
// 粉丝关注获取信息
router.get("/getUsers", async (ctx, next) => {
    try {
        let { user_id } = ctx.query;
        let result = {
            otherUser: [],
            fens:[]
        };
        // 关注我的
        let fens = await sqlQuery(`SELECT u.id,u.avatarUrl,u.nickName,u.language FROM users u JOIN follow f ON u.id=f.user_id AND u.state=1 AND f.state=1 AND f.other_user_id=?`, [user_id]);
        // 我的关注
        let otherUser = await sqlQuery(`SELECT u.id,u.avatarUrl,u.nickName,u.language FROM users u JOIN follow f ON u.id=f.other_user_id AND u.state=1 AND f.state=1 AND f.user_id=?`, [user_id]);
        ctx.body = {
            state: 200,
            message: {
                otherUser,
                fens
            }
        }
    } catch (error) {
        next(error);
    }
})
// 添加朋友获取信息
router.get("/getMyFriends", async (ctx, next) => {
    try {
        let { userid } = ctx.query;
        userid = parseInt(userid);
        let result = {
            myfriendsList: [],
            unMyfriendsList:[]
        };
        // 查询我的朋友
        let fids = await sqlQuery(`SELECT lf.id FROM 
        (SELECT other_user_id id FROM follow WHERE state=1 AND user_id=? ) lf JOIN 
        (SELECT user_id id FROM follow WHERE state=1 AND other_user_id=? ) rf ON lf.id=rf.id`, [userid,userid]);
        let unfid = await sqlQuery(`SELECT id FROM users WHERE id!=? AND state=1 AND id
        NOT IN (SELECT other_user_id id FROM follow WHERE state=1 AND user_id=?)`, [userid,userid]);

        await Promise.all(fids.map(async item => {
            let user = await sqlQuery(`SELECT id,avatarUrl,nickName,(SELECT COUNT(*) count FROM messages WHERE userid=? AND state=0) comment FROM users WHERE state=1 AND id=?`, [item.id,item.id]);
            result.myfriendsList.push(user[0]);
        }))
        await Promise.all(unfid.map(async item => {
            let user = await sqlQuery(`SELECT id,avatarUrl,nickName,(SELECT COUNT(*) count FROM messages WHERE userid=? AND state=0) comment FROM users WHERE state=1 AND id=?`, [item.id,item.id]);
            result.unMyfriendsList.push(user[0]);
        }))
        ctx.body = {
            state: 200,
            message:result
        }
    } catch (error) {
        next(error);
    }
})
// 添加好友
router.get("/addOtherUsers", async (ctx, next) => {
    try {
        let { userid, otheruserid } = ctx.query;
        let result = null;
        userid = parseInt(userid);
        otheruserid = parseInt(otheruserid);
        let count = await sqlQuery(`SELECT COUNT(*) count FROM follow where user_id=? AND other_user_id=?`, [userid, otheruserid]);
        count = count[0].count
        if (count > 0) {
            await sqlQuery(`UPDATE follow SET state=1 WHERE  user_id=? AND other_user_id=?`, [userid, otheruserid]);
        } else {
            await sqlQuery(`INSERT INTO follow(user_id,other_user_id) VALUES(?,?)`, [userid, otheruserid]);
        }
        ctx.body = {
            state: 200,
            message:result
        }
    } catch (error) {
        next(error);
    }
})
// 搜索用户
router.get("/getUserByName", async (ctx, next) => {
    try {
        let { checkid, userid, keyword } = ctx.query;
        parseInt(userid)
        checkid = parseInt(checkid);
        let result = null;
        if (checkid == 0) {
            result = await sqlQuery(`SELECT id,avatarUrl,nickName,language comment FROM users WHERE id!=? AND state=1 AND id
            NOT IN (SELECT other_user_id id FROM follow WHERE state=1 AND user_id=?)
            AND nickName LIKE ?`, [userid,userid,'%' + keyword + '%']);
        } else if (checkid == 1) {
            result = await sqlQuery(`SELECT id,avatarUrl,nickName,language comment FROM users WHERE id!=? AND state=1 AND id
            IN (SELECT other_user_id FROM follow WHERE state=1 AND user_id=?)
            AND nickName LIKE ?`, [userid,userid,'%' + keyword + '%']);
        }
        ctx.body = {
            state: 200,
            message:result
        }
    } catch (error) {
        next(error)
    }
})
// 获取好友
router.get("/findAllFriends", async (ctx, next) => {
    try {
        let { code } = ctx.query;
        let userid = await sqlQuery(`SELECT id FROM users WHERE code=?`, [code]);
        userid = userid[0].id
        userid = parseInt(userid);
        let result = await sqlQuery(`SELECT id,avatarUrl,nickName,language comment FROM users WHERE id!=? AND state=1 AND id
        IN (SELECT lf.id FROM 
        (SELECT other_user_id id FROM follow WHERE state=1 AND user_id=? ) lf JOIN 
        (SELECT user_id id FROM follow WHERE state=1 AND other_user_id=? ) rf ON lf.id=rf.id)`, [userid, userid,userid]);
        await Promise.all(result.map(async item => {
            let count = await sqlQuery(`SELECT COUNT(*) count FROM messages WHERE userid=? AND other_user_id=? AND state=0`, [item.id, userid]);
            item.comment = count[0].count
        }))
        ctx.body = {
            state: 200,
            message:result
        }
    } catch (error) {
        next(error)
    }
})
// 获取单个好友
router.get("/findFriendsById", async (ctx, next) => {
    try {
        let { userid } = ctx.query;
        userid = parseInt(userid);
        let result = await sqlQuery(`SELECT * FROM users WHERE id=? AND state=1`, [userid]);
        ctx.body = {
            state: 200,
            message:result
        }
    } catch (error) {
        next(error)
    }
})

module.exports = router
